﻿package com.dnedelcu.robattle{
	
	import flash.events.Event;
    import flash.events.EventDispatcher;
	import com.dnedelcu.robattle.Player;
	import com.dnedelcu.robattle.GetStatsEvent;

	public class Monster extends EventDispatcher{
		
		private var _characterName:String;
		private var _characterLevel:int;
		private var _currentLife:int;
		private var _maximumLife:int;
		private var _currentMana:int;
		private var _maximumMana:int;
		private var _armor:int;
		private var _minimumAttack:int;
		private var _maximumAttack:int;
		private var _xp:int;
		private var _goldDrop:int;
		private var _itemDrops:Array;
		private var _percentHp:Number;
		private var _percentMana:Number;
		private var _damageReduction:Number;
		
		public function Monster (userInfo:Object) {
			_characterName = userInfo['name'];
			_characterLevel = userInfo['level'];
			_currentLife = userInfo['life'];
			_maximumLife = _currentLife;
			_currentMana = userInfo['mana'];
			_maximumMana = _currentMana;
			_armor = userInfo['armor'];
			_minimumAttack = userInfo['minattack'];
			_maximumAttack = userInfo['maxattack'];
			_xp = userInfo['xp'];
			_goldDrop = userInfo['gold_drop'];
			_itemDrops = new Array(4);
			for (var i=0;i<4;i++) {
				_itemDrops[i] = new Array(2);
				_itemDrops[i][0] = userInfo['item'+i+'_drop_chance'];
				_itemDrops[i][1] = userInfo['item'+i+'_drop_id'];
			}
			_percentHp = _currentLife / _maximumLife;
			if(_maximumMana != 0) {
				_percentMana = _currentMana / _maximumMana;
			} else {
				_percentMana = 0;
			}
			_damageReduction = _armor / (_armor + 400 + 85 * _characterLevel);
		}
		
		public function getRandom(lowNumber, highNumber):int {
			var thisNumber = highNumber - lowNumber;
			var randomUnrounded = Math.random() * thisNumber;
			var randomNumber = Math.round(randomUnrounded);
			randomNumber += lowNumber;
			return randomNumber;
		}
		
		public function Attack(_player:Player):void {
			var rand = getRandom(minimumAttack, maximumAttack);
			var damageReduced = Math.round(rand * _player.damageReduction);
			var damage = rand - damageReduced;
			_player.currentLife = -damage;
			if(_player.currentLife <= 0) {
				_player.currentLife = -_player.currentLife;
				_player.updatePercentHp();
				_player.updatePercentMana();
				var e1:GetStatsEvent = new GetStatsEvent(GetStatsEvent.USER_KILLED, true,true);
				dispatchEvent(e1);
			} else {
				_player.updatePercentHp();
				_player.updatePercentMana();
				var e2:GetStatsEvent = new GetStatsEvent(GetStatsEvent.MONSTER_ATTACKED, true, true);
				dispatchEvent(e2);
			}			
		}
		
		public function get characterName():String {
			return _characterName;
		}

		public function get characterLevel():int {
			return _characterLevel;
		}
		
		public function get currentLife():int {
			return _currentLife;
		}
		
		public function get maximumLife():int {
			return _maximumLife;
		}
		
		public function get currentMana():int {
			return _currentMana;
		}
		
		public function get maximumMana():int {
			return _maximumMana;
		}
		
		public function get armor():int {
			return _armor;
		}
		
		public function get minimumAttack():int {
			return _minimumAttack;
		}

		public function get maximumAttack():int {
			return _maximumAttack;
		}
		
		public function get xp():int {
			return _xp;
		}
		
		public function get goldDrop():int {
			return _goldDrop;
		}
		
		public function get itemDrops():Array {
			return _itemDrops;
		}
		
		public function get percentHp():Number {
			return _percentHp;
		}
		
		public function get percentMana():Number {
			return _percentMana;
		}
		
		public function get damageReduction():Number {
			return _damageReduction;
		}
		
		public function set currentLife(currentLife_:int):void {
			_currentLife += currentLife_;
		}
		
		public function set maximumLife(maximumLife_:int):void {
			_maximumLife += maximumLife_;
		}
		
		public function set currentMana(currentMana_:int):void {
			_currentMana += currentMana_;
		}
		
		public function set maximumMana(maximumMana_:int):void {
			_maximumMana += maximumMana_;
		}
		
		public function set armor(armor_:int):void {
			_armor += armor_;
		}
		
		public function set minimumAttack(minimumAttack_:int):void {
			_minimumAttack += minimumAttack_;
		}
		
		public function set maximumAttack(maximumAttack_:int):void {
			_maximumAttack += maximumAttack_;
		}
		
		public function updatePercentHp():void {
			_percentHp = _currentLife / _maximumLife;
		}
		
		public function updatePercentMana():void {
			_percentMana = _currentMana / _maximumMana;
		}
		
		public function setDamageReduction():void {
			_damageReduction = _armor / (_armor + 400 + 85 * _characterLevel);
		}
	}
}